
#ifndef IOUNIT_H_
#define IOUNIT_H_

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

/**
\class IOUnit
\brief Class for Input/Output File Operations.

A little class for writing and reading from files in binary or ascii mode.
*/
class IOUnit
{

public:
	/** Constructor*/
	IOUnit();
	/** Descturctor*/
	~IOUnit();

	/** Checks if a file is ready to open.
	\param filename The name of the file which has to opened.
	\return True if the file is ok, flase if not.
	*/
	bool checkFile(char* filename);

	void readFromFile(char* filename);

	/** Function for reading a file in binary mode (e.g. for generated random bits).
	\param filename Name of the file.
	\param offset Offset for the filepointer (e.g. for skip some bytes of the file)
	\return A Pointer to an internal result vector, which contains the read bytes.
	*/
	std::vector<short>* readFromFile(char* filename, int offset);

	/** Writes data into a file.
	\param filename Name of the file to write to.
	\param data Array of type char, which contains the data.
	\param size Number of bytes to write.
	*/
	void writeToFile(char* filename, char* data, int size, bool binary);


	/** After using readFromFile(char*, int), you can use this function to get
	the number of read bits from the file.
	\return Number of bits of the last read file.
	*/
	int getBitCount(){ return bitCount; }

private:

	/** Input file stream for reading files*/
	std::ifstream pIFile;
	/** Output file stream for writing to files*/
	std::ofstream pOFile;
	/** Streambuffer for input operations*/
	std::streambuf* pIbuf;
	/** Streambuffer for output operations*/
	std::streambuf* pObuf;
	/** Contains the binary data from a read file after using readFromFile(char*, int)*/
	std::vector<short> bitVector;
	/** Contains the number of read bits, after using readFromFrile(char*, int)*/
	int bitCount;
	/** Temp var for peeking*/
	char nextChar;

	std::string* outputstring;
	char* inputbuffer;

};

#endif


